﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NewGlobalBizAssist
{
    public partial class Members : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Enable the GridView paging option and  
                // specify the page size. 
                gvMembers.AllowPaging = true;
                gvMembers.PageSize = 15;


                // Enable the GridView sorting option. 
                gvMembers.AllowSorting = true;


                // Initialize the sorting expression. 
                ViewState["SortExpression"] = "ElementID";


                // Populate the GridView. 
                BindGridView();
            } 

        }

        private void BindGridView()
        {
            Lib.DataAccess da = new Lib.DataAccess();
            DataSet ds = da.GetDataSet("select * from Member");
            gvMembers.DataSource = ds;
            gvMembers.DataBind();
         
        }

        // GridView.PageIndexChanging Event 
        protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            // Set the index of the new display page.  
            gvMembers.PageIndex = e.NewPageIndex;


            // Rebind the GridView control to  
            // show data in the new page. 
            BindGridView();
        }

        protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // Make sure the current GridViewRow is a data row. 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Make sure the current GridViewRow is either  
                // in the normal state or an alternate row. 
                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
                {
                    // Add client-side confirmation when deleting. 
                    ((Button)e.Row.Cells[0].Controls[0]).Attributes["onclick"] = "if(!confirm('Are you certain you want to delete this member ?')) return false;";
                    ((HyperLink)e.Row.Cells[1].Controls[1]).NavigateUrl = "~/MemberInfo.aspx?ID=" + ((HyperLink)e.Row.Cells[1].Controls[1]).Text;
                }
            }
        }


        // GridView.Sorting Event 
        protected void gv_Sorting(object sender, GridViewSortEventArgs e)
        {
            string[] strSortExpression = ViewState["SortExpression"].ToString().Split(' ');


            // If the sorting column is the same as the previous one,  
            // then change the sort order. 
            if (strSortExpression[0] == e.SortExpression)
            {
                if (strSortExpression[1] == "ASC")
                {
                    ViewState["SortExpression"] = e.SortExpression + " " + "DESC";
                }
                else
                {
                    ViewState["SortExpression"] = e.SortExpression + " " + "ASC";
                }
            }
            // If sorting column is another column,   
            // then specify the sort order to "Ascending". 
            else
            {
                ViewState["SortExpression"] = e.SortExpression + " " + "ASC";
            }


            // Rebind the GridView control to show sorted data. 
            BindGridView();
        }


        protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            gvMembers.Rows[e.RowIndex].Visible = false;
        }

        protected void btn_Click(object sender, EventArgs e)
        {
            Response.Redirect("MemberInfo.aspx");
        }
    }
}